package cn.edu.ahtcm.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.edu.ahtcm.util.DBUtil;

public class UserDao {

    public static void main(String[] args) throws SQLException {
        //查询
        Connection conn = DBUtil.getConnection();
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select * from users");
        while(rs.next()){
            System.out.println(rs.getString(2));
        }
        //插入
        Boolean s = st.execute("insert into users(name,password) values('zhang','123456')");
        //删除id为2的记录
        st.execute("delete from users where id=2");
        //更新操作(预备义语句，防止sql注入)
        PreparedStatement ps = conn.prepareStatement("update users set name=? where id=5");
        ps.setString(1, "李四");
        ps.execute();
        DBUtil.release(rs, st, ps, conn);
    }

}
